package com.zhuqieli.database_learn.service;

import com.alibaba.fastjson.JSON;
import com.zhuqieli.database_learn.config.RabbitmqConfig;
import com.zhuqieli.database_learn.pojo.MqMessage;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

@Service
public class MqService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    FileTableService fileTableService;

    //发送消息
    public void setMessage(MqMessage mqMessage) {

        //使用rabbitTemplate发送对象
        /**
         * 参数：
         * 1队列名称
         * 2消息
         */
//        MqMessage mqMessage=new MqMessage();
//        mqMessage.setFileName("test");
        String message = JSON.toJSONString(mqMessage);
        rabbitTemplate.convertAndSend(RabbitmqConfig.QUEUE_INFORM_DOWNLOAD, message);
    }

    //监听下载队列
    @RabbitListener(queues = {RabbitmqConfig.QUEUE_INFORM_DOWNLOAD})
    public void receive_download(@Payload String message) {//MqMessage message
        MqMessage mqMessage = JSON.parseObject(message, MqMessage.class);
        //调用下载方法
        System.out.println(mqMessage.toString());
        fileTableService.fileDownload(mqMessage.getAccount(), mqMessage.getFileId());
//        System.out.println(mqMessage.getFileName());
//        System.out.println(mqMessage.getResponse());
    }

    //监听上传队列
    @RabbitListener(queues = {RabbitmqConfig.QUEUE_INFORM_UPLOAD})
    public void receive_upload(@Payload String message) {

    }

}
